package io.gitee.chearnee.fw.common.component;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/6/23 11:30
 */
public class CustomLocalDateTimeSerializer extends StdSerializer<LocalDateTime> {

  public CustomLocalDateTimeSerializer() {
    this(null);
  }

  protected CustomLocalDateTimeSerializer(Class<LocalDateTime> t) {
    super(t);
  }

  @Override
  public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator,
      SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeNumber(LocalDateTimeUtil.toEpochMilli(localDateTime));
  }
}